package io.github.uicdb.et.block;

import io.github.uicdb.et.ElementalTheory;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.FastColor;
import net.minecraft.world.entity.player.Inventory;
import org.jetbrains.annotations.NotNull;

public class TurnInScreen extends AbstractContainerScreen<TurnInMenu> {
    private static final ResourceLocation CONTAINER_BACKGROUND = ResourceLocation.fromNamespaceAndPath(ElementalTheory.MODID, "textures/gui/turn_in_background.png");
        int progressStartX=26, progressStartY=108,progressEndX=149, progressEndY=111;
        int length = progressEndX-progressStartX;
    public TurnInScreen(TurnInMenu menu, Inventory playerInventory, Component title) {
        super(menu, playerInventory, title);

        this.imageHeight = 196;
        this.imageWidth = 176;
        this.inventoryLabelY = this.imageHeight - 94;

    }

    @Override
    protected void init() {
        super.init();
        this.topPos = (this.height - this.imageHeight) / 2;
        this.leftPos = (this.width - this.imageWidth) / 2;
        addRenderableWidget(new Button.Builder(Component.translatable("gui.turn_in_screen.craft"), new Button.OnPress() {
            @Override
            public void onPress(@NotNull Button button) {
                ICraft.requireCraft(menu.getPos());
            }
        }).bounds(leftPos + 136, topPos + 99, 32, 12).build());
    }

    public void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
        super.render(guiGraphics, mouseX, mouseY, partialTick);

        renderInfo(guiGraphics, 0, 101, 15);
        renderInfo(guiGraphics, 1, 145, 52);
        renderInfo(guiGraphics, 2, 127, 85);
        renderInfo(guiGraphics, 3, 32, 85);
        renderInfo(guiGraphics, 4, 22, 30);
        renderInfo(guiGraphics, 5, 96, 78);
        renderInfo(guiGraphics, 6, 64, 42);
        this.renderTooltip(guiGraphics, mouseX, mouseY);
    }

    private void renderInfo(@NotNull GuiGraphics guiGraphics, int index, int x, int y) {
        guiGraphics.drawString(minecraft.font, menu.getElementContainerData().get(index) + "", leftPos + x, topPos + y, ChatFormatting.GOLD.getColor());
    }

    private float getProgress() {
        int max = menu.getRecipeContainerData().get(1);
        int recipeTime = menu.getRecipeContainerData().get(0);
        if (max == 0) return 0;
        return (float) recipeTime / (float) max;
    }

    protected void renderBg(GuiGraphics guiGraphics, float partialTick, int mouseX, int mouseY) {
        guiGraphics.blit(CONTAINER_BACKGROUND, leftPos, topPos, this.imageWidth, this.imageHeight, 0, 0, this.imageWidth, this.imageHeight, this.imageWidth, this.imageHeight);
        guiGraphics.fill(leftPos+progressStartX,topPos+progressStartY,(int)(leftPos+progressStartX+length*getProgress()),topPos+progressEndY, FastColor.ARGB32.color(255,255,0,0));
    }
}
